package com.md.model.base;

import com.md.util.ObjectUtil;
import com.md.value.constants.IOConstants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.util.UUID;

/**
 * @author 刘骄阳
 * 2023-03-02 17:52
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
public class FileStream implements AutoCloseable {
  private String fileName;
  private byte[] fileData;
  private String password;
  private Long size;
  private InputStream inputStream;


  public FileStream(final File file) throws IOException {
    try {
      this.inputStream = Files.newInputStream(file.toPath());
      this.fileName = file.getName().toLowerCase();
    } catch (Exception e) {
      log.error("获取文件流失败: {}", e.getMessage(), e);
      close();
    }
  }

//  public FileStream(final MultipartFile multipartFile) throws IOException {
//    try {
//      this.inputStream = multipartFile.getInputStream();
//      this.fileName = (StringUtil.getVal(multipartFile.getOriginalFilename()).toLowerCase());
//    } catch (Exception e) {
//      log.error("获取文件流失败: {}", e.getMessage(), e);
//      close();
//    }
//  }

  public FileStream(final String fileUrl) throws IOException {
    URL url = new URL(fileUrl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    //设置超时间为3秒
    conn.setConnectTimeout(IOConstants.CONNECTION_MAX_TIMEOUT);
    //防止屏蔽程序抓取而返回403错误
    conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
    try {
      this.inputStream = conn.getInputStream();
      String suffix = fileUrl.substring(fileUrl.lastIndexOf("."));
      this.fileName = UUID.randomUUID() + suffix;
    } catch (Exception e) {
      log.error("获取文件流失败: {}", e.getMessage(), e);
      close();
    }
  }

  public FileStream(final XWPFPictureData pictureData) {
    try {
      this.inputStream = pictureData.getPackagePart().getInputStream();
      this.fileName = pictureData.getFileName();
      this.size = pictureData.getChecksum();
    } catch (Exception e) {
      log.error("获取文件流失败: {}", e.getMessage(), e);
      close();
    }
  }

  @Override
  public void close() {
    if (ObjectUtil.notEmpty(inputStream)) {
      try {
        inputStream.close();
      } catch (IOException e) {
        log.error("关闭流时发生异常: {}", e.getMessage(), e);
      }
    }
    inputStream = null;
    fileData = null;
  }

//  public static FileStream of(final MultipartFile multipartFile,
//                              final File file,
//                              final String url) throws IOException {
//    if (multipartFile != null) {
//      return new FileStream(multipartFile);
//    } else if (file != null) {
//      return new FileStream(file);
//    } else {
//      return new FileStream(url);
//    }
//  }
}
